<%@ page language="java" contentType="text/html; charset=UTF-8"
         pageEncoding="UTF-8" %>
<script type="text/javascript">
    $.getJSON('/admin/auth_menus.action?mpid=${mpid}', function (data) {
        var ul = document.createElement("ul");
        ul.setAttribute("class", "menu");
        var menu = transData(data, 'sid', 'mPno', 'child');
        for (var i in menu) {
        	if('${sessionScope.user.rLevel}'==3 || '${sessionScope.user.rLevel}'==4){
            	menus(ul, menu[i]);
            }else if('${mpid}'!=''){
	            if(menu[i].sid=='${mpid}'){
	            	menus(ul, menu[i]);
	            }
            }else {
            	if(menu[i].sid=='${sessionScope.mpid}'){
                	menus(ul, menu[i]);
                }
            }
        }
        var left_menu = document.getElementById("menus");
        left_menu.appendChild(ul);
        var $a_list = $(left_menu).find("a");
        $a_list.each(function () {
            var $a = $(this);
            $a.click(function () {
                var $sub_menu = $(this).next("ul");
                if ($sub_menu.length) {//有子菜单
                    expand($a, $sub_menu);
                    if($a.attr("href") == ""){//菜单无连接
                    	return false;
                    }else{
                    	return true;
                    }
                } else {
                    return true;
                }
            })
        });


        var locationHref = window.location.href;
        $(".menu a").each(function () {
            //if (locationHref.indexOf($(this).attr("href")) > 0) {
            if (locationHref.endWith($(this).attr("href"))) {
                $(this).addClass("active");
                var $hasChild =  $(this).parent().find("ul");
                if($(this).attr("value") == 0){//一级菜单
                	var $sub_menu = $(this).next("ul");
                	if($sub_menu.length){//有子菜单,展开上级和下级
                		$(this).find("span:last").attr("class", "d-r fa fa-chevron-up");
                		$(this).next("ul").attr("aria-expanded", true);
                		$(this).next("ul").attr("style", "display: block;");
                	}
                }
                if($(this).attr("value") == 1){//二级菜单
                	var $sub_menu = $(this).next("ul");
                	if($sub_menu.length){//有子菜单,展开上级和下级
                		$(this).find("span:last").attr("class", "d-r fa fa-chevron-up");
                		$(this).next("ul").attr("aria-expanded", true);
                		$(this).next("ul").attr("style", "display: block;");
                	}
               		$(this).parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
					var parent = $(this).parent().parent().prev().find("span:first").text();
					var my = $(this).find("span:first").text();
					var rightTopMsg = "管理后台"+" > "+parent+" > "+my;
                    $("#rightTopMsg").text(rightTopMsg);
               		return;
                }
            	if($(this).attr("value") == 2){//三级菜单点击，展开前两级
                    var $sub_menu = $(this).next("ul");
                    if($sub_menu.length){//如果有子菜单
                    	$(this).find("span:last").attr("class", "d-r fa fa-chevron-up");
                		$(this).next("ul").attr("aria-expanded", true);
                		$(this).next("ul").attr("style", "display: block;");
					}
            		$(this).parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                    $(this).parent().parent().parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent().parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                    var parentp = $(this).parent().parent().parent().parent().prev().find("span:first").text();
                    var parent = $(this).parent().parent().prev().find("span:first").text();
					var my = $(this).find("span:first").text();
					var rightTopMsg = "管理后台"+" > "+parentp+" > "+parent+" > "+my;
                    $("#rightTopMsg").text(rightTopMsg);
            		return;
            	}
            	if($(this).attr("value") == 3){//四级菜单点击，展开前三级
            		$(this).parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                    $(this).parent().parent().parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent().parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                    $(this).parent().parent().parent().parent().parent().parent("ul[aria-expanded='false']").show();
                    $(this).parent().parent().parent().parent().parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                    $(this).parent().parent().parent().parent().parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                    var parentpp = $(this).parent().parent().parent().parent().parent().parent().prev().find("span:first").text();
                    var parentp = $(this).parent().parent().parent().parent().prev().find("span:first").text();
                    var parent = $(this).parent().parent().prev().find("span:first").text();
					var my = $(this).find("span:first").text();
					var rightTopMsg = "管理后台"+" > "+parentpp+" > "+parentp+" > "+parent+" > "+my;
                    $("#rightTopMsg").text(rightTopMsg);
            		return;
            	}
                $(this).parent().parent("ul[aria-expanded='false']").show();
                $(this).parent().parent("ul[aria-expanded='false']").attr("aria-expanded", true);
                $(this).parent().parent().prev().find("span:last").attr("class", "d-r fa fa-chevron-up");
                var my = $(this).find("span:first").text();
				var rightTopMsg = "管理后台"+" > "+my;
                $("#rightTopMsg").text(rightTopMsg);
            }
        });
        //如果未一级菜单，并且没有链接，则点开
        for (var i in menu) {
            if('${mpid}'!=''){
	            if(menu[i].sid=='${mpid}'){
	            	if(menu[i].mPno=='0' && menu[i].mUrl=='' && '${first}'=='first'){
		            		$(".menu li:first a:first").click();
		            }
	            }
            }else{
            	if(menu[i].sid=='${sessionScope.mpid}'){
            		if(menu[i].mPno=='0' && menu[i].mUrl=='' && '${first}'=='first'){
	            		$(".menu li:first a:first").click();
	            	}
                }
            }
        }
    });

    function expand($a, $sub_menu) {
        $sub_menu.slideToggle(500, function () {
        	var expand = $(this).attr("aria-expanded");
        	if($(this).prev().attr("value") == 2){//三级菜单点击
        		if (expand == "true") {//已伸展
	                $(this).attr("aria-expanded", false);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-down");
	            }else{
	        		$(this).attr("aria-expanded", true);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-up");
	            }
        		return;
        	}else if($(this).prev().attr("value") == 1){//二级菜单点击
        		if (expand == "true") {//已伸展
	                $(this).attr("aria-expanded", false);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-down");
	            }else{
	        		$(this).attr("aria-expanded", true);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-up");
	            }
        		return;
        	}else{//一级菜单
	            if (expand == "true") {//已伸展
	                $(this).attr("aria-expanded", false);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-down");
	            } else {
	                var $expand_ul = $("ul[aria-expanded='true']");
	                if ($expand_ul.length) {
	                    var $expand_a = $expand_ul.prev("a");
	                    $expand_a.find("span:last").attr("class", "d-r fa fa-chevron-down");
	                    $expand_ul.attr("aria-expanded", false);
	                    $expand_ul.slideUp(100);
	                }
	                $(this).attr("aria-expanded", true);
	                $a.find("span:last").attr("class", "d-r fa fa-chevron-up");
	            }
        	}
        });
    }


    /**
     * ç»ç»tree html
     * @param ul
     * @param menu
     */
    function menus(ul, menu) {
        var li = document.createElement("li");
        if (menu.child) {
            var children = menu.child;
            var e = document.createElement("ul");
            e.setAttribute("class", "back_menu_sub");
            e.setAttribute("aria-expanded", false);
            var a = document.createElement("a");
            a.setAttribute("value", menu.mLevel);
            var icon = document.createElement("i");
            icon.setAttribute("class", menu.mIcon);
            if (menu.mLevel == 2) {
            	icon.setAttribute("style", "margin-left:25px");
			}
            if (menu.mLevel == 3) {
            	icon.setAttribute("style", "margin-left: 48px;");
			}
            var span = document.createElement("span");
            span.appendChild(document.createTextNode(menu.mName));
            var arrow = document.createElement("span");
            arrow.setAttribute("class", "d-r fa fa-chevron-down");
            a.setAttribute("href", menu.mUrl);
            a.appendChild(icon);
            a.appendChild(span);
            a.appendChild(arrow);
            li.appendChild(a);
            li.appendChild(e);
            for (var i in children) {
                menus(e, children[i]);
            }
        } else {
            var a = document.createElement("a");
            a.setAttribute("href", menu.mUrl);
            a.setAttribute("value", menu.mLevel);
            var icon = document.createElement("i");
            icon.setAttribute("class", menu.mIcon);
            if (menu.mLevel == 2) {
            	icon.setAttribute("style", "margin-left:25px");
			}
            if (menu.mLevel == 3) {
            	icon.setAttribute("style", "margin-left: 48px;");
			}
            var span = document.createElement("span");
            span.appendChild(document.createTextNode(menu.mName));
            a.appendChild(icon);
            a.appendChild(span);
            a.setAttribute("data", menu.mPno);
            li.appendChild(a);
        }
        ul.appendChild(li);
    }

    /**
     * å°æ°æ®è½¬ætreeç»æ
     * @param a
     * @param idStr
     * @param pidStr
     * @param chindrenStr
     * @returns {Array}
     */
    function transData(a, idStr, pidStr, chindrenStr) {
        var r = [], hash = {}, id = idStr, pid = pidStr, children = chindrenStr, i = 0, j = 0, len = a.length;
        for (; i < len; i++) {
            hash[a[i][id]] = a[i];
        }
        for (; j < len; j++) {
            var aVal = a[j], hashVP = hash[aVal[pid]];
            if (hashVP) {
                !hashVP[children] && (hashVP[children] = []);
                hashVP[children].push(aVal);
            } else {
                r.push(aVal);
            }
        }
        return r;
    }

     
    String.prototype.endWith=function(s){     
    	  if(s==null||s==""||this.length==0||s.length>this.length)
    		     return false;
    		  if(this.substring(this.length-s.length)==s)
    		     return true;
    		  else
    		     return false;
    		  return true;       
    	}
</script>
